//获取某笔费用的可报金额
import React from 'react';
import { messages, queryCurrencyPrecision } from 'share/common';

const getAbleRelateAmt = (info, invoiceOID) => {
  let writeOffAmt = 0;
  info.bankTrxInvoiceRelations && info.bankTrxInvoiceRelations.map(item => {
    item.invoiceOID === invoiceOID && (writeOffAmt += item.writeOffAmt);
  });
  let balanceAmount = info.balanceAmount || 0;
  return `${info.posCurCod} ${writeOffAmt + balanceAmount}`;
};

function Cell(props) {
  return <span>{props.value}</span>;
}

const formatTime = (time) => {
  return `${time.substr(0, 2)}:${time.substr(2, 2)}:${time.substr(4, 2)}`;
};

//商务卡消费类型
const typeMap = new Map([
  ['00', messages('expense-1.key27')/*一般消费*/],
  ['01', messages('expense-1.key28')/*预借现金*/],
  ['12', messages('expense-1.key29')/*预借现金退货*/],
  ['20', messages('expense-1.key30')/*一般消费退货*/],
  ['60', messages('expense-1.key31')/*还款及费用*/]
]);

const BUSINESS_MAP = {
  headerTitle: {
    name: messages('expense-1.key706')/*消费名称*/,
    render: (value, record) => <Cell value={record.acpName}/>
  },
  origin: {
    name: messages('common.from')/*来源*/,
    render: (value, record) => record.cardTypeCode
  },
  totalAmount: {
    name: messages('common.amount')/*金额*/,
    render: (value, record, invoiceOID) => <Cell value={getAbleRelateAmt(record, invoiceOID)}/>
  },
  acpName: {
    name: messages('expense-1.key706')/*消费名称*/,
    render: (value, record) => <Cell value={value}/>
  },
  balanceAmount: {
    name: messages('expense-1.key35')/*可报金额*/,
    render: (value, record) => <Cell value={getAbleRelateAmt(record)}/>
  },
  posCurAmt: {
    name: messages('expense-1.key36')/*入账金额*/,
    render: (value, record) => <Cell
      value={`${record.posCurCod} ${value.toFixed(queryCurrencyPrecision(record.posCurCod))}`}/>
  },
  oriCurAmt: {
    name: messages('expense-1.key37')/*交易金额*/,
    render: (value, record) => <Cell
      value={`${record.oriCurCod} ${value.toFixed(queryCurrencyPrecision(record.oriCurCod))}`}/>
  },
  trsDate: {
    name: messages('expense-1.key707')/*交易时间*/,
    render: (value, record) => <Cell value={`${value} ${formatTime(record.trxTim)}`}/>
  },
  crdNum: {
    name: messages('expense-1.key41')/*交易卡号*/,
    render: (value, record) => <Cell
      value={`${record.bankName} ${value ? messages('expense-1.key42')/*尾号*/ + ' ' + (value + '0').slice(-5, -1) : ''}`}/>
  },
  bilMon: {
    name: messages('expense-1.key40')/*账单月*/,
    render: (value, record) => <Cell value={value || '-'}/>
  },
  trsCod: {
    name: messages('expense-1.key43')/*交易类型*/,
    render: (value, record) => <Cell value={typeMap.get(value) || '-'}/>
  },
  remark: {
    name: messages('common.remark')/*备注temp*/,
    render: (value, record) => <Cell value={value || '-'}/>
  },
  cardTypeCode: {
    name: messages('common.from')/*来源*/,
    render: (value, record) => <Cell value={value || '-'}/>
  }
};

export default BUSINESS_MAP;
